package com.example.cachedemo.service.impl;

import com.example.cachedemo.mapper.CheckMapper;
import com.example.cachedemo.service.CheckService;
import com.example.cachedemo.service.dto.ClassInfo;
import com.example.cachedemo.service.dto.check.ClassCheckAbsentDTO;
import com.example.cachedemo.service.dto.check.ClassCheckRatioDTO;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class CheckServiceImpl implements CheckService {
    private final CheckMapper checkMapper;
    private Log logger = LogFactory.getLog("CheckServiceImpl");

    public CheckServiceImpl(CheckMapper checkMapper) {
        this.checkMapper = checkMapper;
    }

    @Override
    @Cacheable(cacheNames = "system", key = "#className+'-'+#termNo")
    public ClassCheckRatioDTO getClassCheckRatio(String className, String termNo) {
        this.logger.info(String.format("获取班级出勤率方法启动，" +
                "输入参数：%s,%s", className, termNo));
        long time0 = System.currentTimeMillis();
        //1、获取班级信息
        ClassInfo classInfo = this.checkMapper.getClassInfoByClassName(className);
        //模拟数据延迟
        try {
            Thread.sleep(1500l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time1 = System.currentTimeMillis();
        this.logger.info(String.format("查询班级信息耗时:%sms", (time1 - time0)));
        //2.查询班级出勤率
        Float classRatio = this.checkMapper.getClassRatioByClassNameAndTerm(classInfo.getClassNo(), termNo);
        //模拟数据延迟
        try {
            Thread.sleep(1500l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time2 = System.currentTimeMillis();
        this.logger.info(String.format("查询班级出勤率耗时：%sms", (time2 - time1)));

        //3.查询部门出勤率
        Float depRatio = this.checkMapper.getDepRatioByDepNoAndTerm(classInfo.getDepartmentInfo()
                .getDepNo(), termNo);
        //模拟数据延迟
        try {
            Thread.sleep(1500l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time3 = System.currentTimeMillis();
        this.logger.info(String.format("查询部门出勤率耗时：%sms", (time3 - time2)));

        //4.组装结果类型
        ClassCheckRatioDTO result = new ClassCheckRatioDTO(classInfo, termNo, classRatio, depRatio);
        long time4 = System.currentTimeMillis();
        this.logger.info(String.format("获取班级出勤率方法完成，总用时：%sms", (time4 - time0)));
        return result;
    }

    @Override
    @Cacheable(cacheNames = "system", key = "#className+'-'+#termNo")
    public ClassCheckAbsentDTO getClassCheckAbsent(String className, String termNo) {
        this.logger.info(String.format("获取班级旷课总人数方法启动，" +
                "输入参数：%s,%s", className, termNo));
        long time0 = System.currentTimeMillis();
        //1、获取班级信息
        ClassInfo classInfo = this.checkMapper.getClassInfoByClassName(className);
        //模拟数据延迟
        try {
            Thread.sleep(1500l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time1 = System.currentTimeMillis();
        this.logger.info(String.format("查询班级信息耗时:%sms", (time1 - time0)));
        //2.查询班级旷课总人数
        Integer classAbsentCount = this.checkMapper.getClassAbsentByClassNameAndTerm(classInfo.getClassNo(), termNo);
        //模拟数据延迟
        try {
            Thread.sleep(1500l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time2 = System.currentTimeMillis();
        this.logger.info(String.format("查询班级旷课总人数耗时：%sms", (time2 - time1)));

        //3.查询部门旷课总人数
        Integer depAbsentCount = this.checkMapper.getDepAbsentByDepNoAndTerm(classInfo.getDepartmentInfo()
                .getDepNo(), termNo);
        //模拟数据延迟
        try {
            Thread.sleep(1500l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time3 = System.currentTimeMillis();
        this.logger.info(String.format("查询部门旷课总人数耗时：%sms", (time3 - time2)));

        //4.组装结果类型
        ClassCheckAbsentDTO result = new ClassCheckAbsentDTO(classInfo, termNo, classAbsentCount, depAbsentCount);
        long time4 = System.currentTimeMillis();
        this.logger.info(String.format("获取班级旷课总人数方法完成，总用时：%sms", (time4 - time0)));
        return result;
    }
}